package cn.eric.game.swing;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TestCheckBox {
	private JFrame mainFrame;
	   private JLabel headerLabel;
	   private JLabel statusLabel;
	   private JPanel controlPanel;
	   private JPanel picPanel;
	   
	   public TestCheckBox(){
	      prepareGUI();
	   }

	   public static void main(String[] args){
		  TestCheckBox swingControlDemo = new TestCheckBox();      
	      swingControlDemo.showCheckBoxDemo();
	   }

	   private void prepareGUI(){
	      mainFrame = new JFrame("Java Swing Examples");
	      mainFrame.setSize(400,400);
	      mainFrame.setLayout(new GridLayout(4, 1));
	      mainFrame.addWindowListener(new WindowAdapter() {
	         public void windowClosing(WindowEvent windowEvent){
	            System.exit(0);
	         }        
	      });    
	      headerLabel = new JLabel("", JLabel.CENTER);        
	      statusLabel = new JLabel("",JLabel.CENTER);    

	      statusLabel.setSize(350,100);

	      controlPanel = new JPanel();
	      controlPanel.setLayout(new FlowLayout());

	      picPanel = new JPanel();
	      ImageIcon iconLiu = createImageIcon("/resources/liubei.jpg","Java");
	      iconLiu.setImage(iconLiu.getImage().getScaledInstance(80, 80,  
                  Image.SCALE_DEFAULT));
	      JLabel labelLiu = new JLabel("", iconLiu,JLabel.CENTER);
	      ImageIcon iconCao = createImageIcon("/resources/caocao.jpg","Java");
	      iconCao.setImage(iconCao.getImage().getScaledInstance(80, 80,  
	    		  Image.SCALE_DEFAULT));
	      JLabel labelCao = new JLabel("", iconCao,JLabel.CENTER);
	      ImageIcon iconSun = createImageIcon("/resources/sunquan.jpg","Java");
	      iconSun.setImage(iconSun.getImage().getScaledInstance(80, 80,  
	    		  Image.SCALE_DEFAULT));
	      JLabel labelSun = new JLabel("", iconSun,JLabel.CENTER);
	      ImageIcon iconDong = createImageIcon("/resources/dongzhuo.jpg","Java");
	      iconDong.setImage(iconDong.getImage().getScaledInstance(80, 80,  
	    		  Image.SCALE_DEFAULT));
	      JLabel labelDong = new JLabel("", iconDong,JLabel.CENTER);
	      picPanel.setLayout(new FlowLayout(2, 20, 2));
	      picPanel.add(labelLiu);
	      picPanel.add(labelCao);
	      picPanel.add(labelSun);
	      picPanel.add(labelDong);
	      
	      mainFrame.add(headerLabel);
	      mainFrame.add(controlPanel);
	      mainFrame.add(picPanel);
	      mainFrame.add(statusLabel);
	      mainFrame.setVisible(true);  
	   }
	  
	   private static ImageIcon createImageIcon(String path, 
			      String description) {
			      java.net.URL imgURL = TestButton.class.getResource(path);
			      if (imgURL != null) {
			         return new ImageIcon(imgURL, description);
			      } else {            
			         System.err.println("Couldn't find file: " + path);
			         return null;
			      }
		}  
	   
	   private void showCheckBoxDemo(){

	      headerLabel.setText("请选择出战的君主"); 

	      final JCheckBox chkLiu = new JCheckBox("刘备");
	      final JCheckBox chkCao = new JCheckBox("曹操");
	      final JCheckBox chkSun = new JCheckBox("孙权");
	      final JCheckBox chkDong = new JCheckBox("董卓");
	      
	      chkLiu.setMnemonic(KeyEvent.VK_L);
	      chkCao.setMnemonic(KeyEvent.VK_C);
	      chkSun.setMnemonic(KeyEvent.VK_S);
	      chkDong.setMnemonic(KeyEvent.VK_D);

	      chkLiu.addItemListener(new ItemListener() {
	         public void itemStateChanged(ItemEvent e) {         
	            statusLabel.setText("您选择了刘备: " 
	            + (e.getStateChange()==1?"checked":"unchecked"));
	         }           
	      });

	      chkCao.addItemListener(new ItemListener() {
	         public void itemStateChanged(ItemEvent e) {             
	            statusLabel.setText("您选择了曹操: " 
	            + (e.getStateChange()==1?"checked":"unchecked")); 
	         }           
	      });

	      chkSun.addItemListener(new ItemListener() {
	         public void itemStateChanged(ItemEvent e) {             
	            statusLabel.setText("您选择了孙权: " 
	            + (e.getStateChange()==1?"checked":"unchecked"));
	         }           
	      });
	      
	      chkDong.addItemListener(new ItemListener() {
	    	  public void itemStateChanged(ItemEvent e) {             
	    		  statusLabel.setText("您选择了董卓: " 
	    				  + (e.getStateChange()==1?"checked":"unchecked"));
	    	  }           
	      });
	      controlPanel.setLayout(new FlowLayout(1, 30, 2));
	      controlPanel.add(chkLiu);
	      controlPanel.add(chkCao);
	      controlPanel.add(chkSun);       
	      controlPanel.add(chkDong);       
	      
	      mainFrame.setVisible(true);  
	   }
}
